// ChartControl.cs
// 
// Copyright (C) 2008 Jean-André Santoni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using Gtk;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Astrognome
{
	public partial class ChartControl : Gtk.Bin
	{
		private Chart Chart;
		private MainWindow parent;
		string appdir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
		
		public ChartControl(MainWindow mainwindow, Chart chart)
		{
			this.Build();
			
			parent = mainwindow;
			Chart = chart;
			
			Dictionary<double, int> dic = new Dictionary<double,int> ();
			dic.Add(12,      0);
			dic.Add(11,      1);
			dic.Add(10,      2);
			dic.Add(9.5,     3);
			dic.Add(9,       4);
			dic.Add(8,       5);
			dic.Add(7,       6);
			dic.Add(6,       7);
			dic.Add(5,       8);
			dic.Add(4.5,     9);
			dic.Add(4,      10);
			dic.Add(3.5,    11);
			dic.Add(3,      12);
			dic.Add(2,      13);
			dic.Add(1,      14);
			dic.Add(0,      15);
			dic.Add(-1,     16);
			dic.Add(-2,     17);
			dic.Add(-3,     18);
			dic.Add(-3.5,   19);
			dic.Add(-4,     20);
			dic.Add(-4.5,   21);
			dic.Add(-5,     22);
			dic.Add(-5.5,   23);
			dic.Add(-5.75,  24);
			dic.Add(-6,     25);
			dic.Add(-6.5,   26);
			dic.Add(-7,     27);
			dic.Add(-8,     28);
			dic.Add(-8.75,  29);
			dic.Add(-9,     30);
			dic.Add(-9.5,   31);
			dic.Add(-10,    32);
			dic.Add(-10.5,  33);
			dic.Add(-11,    34);
			dic.Add(-11.5,  35);
			dic.Add(-12,    36);
			dic.Add(-12.75, 37);
			dic.Add(-13,    38);
			dic.Add(-14,    39);
			
			tzCombobox.Active = dic[chart.TimeZone];
						
			countryEntry.Completion = new EntryCompletion();
			ListStore store = new ListStore(typeof(string));
			countryEntry.Completion.Model = store;
			countryEntry.Completion.TextColumn = 0;
			
			DirectoryInfo di = new DirectoryInfo(appdir + "/cities");
			FileInfo[] rgFiles = di.GetFiles("*.csv");
			foreach(FileInfo fi in rgFiles)
			{
				store.AppendValues(fi.Name.Split('.')[0]);
			}
			
			nameEntry.Text =         Chart.Name;
			countryEntry.Changed +=  OnCountryEntryChanged;
			countryEntry.Text =      Chart.Location.Country;
			cityEntry.Text =         Chart.Location.City;
			dateCalendar.Date =      Chart.DateTime;
			hourSpinbutton.Value =   Chart.DateTime.Hour;
			minuteSpinbutton.Value = Chart.DateTime.Minute;
			secondSpinbutton.Value = Chart.DateTime.Second;
			dstCheckbutton.Active =  Chart.DST;
			
			nameEntry.Changed +=        OnNameEntryChanged;
			cityEntry.Changed +=        OnCityEntryChanged;
			tzCombobox.Changed +=       OnTzComboboxChanged;
			dateCalendar.DaySelected += OnDateChanged;
			hourSpinbutton.Changed +=   OnDateChanged;
			minuteSpinbutton.Changed += OnDateChanged;
			secondSpinbutton.Changed += OnDateChanged;
			dstCheckbutton.Clicked +=   OnDstCheckbuttonClicked;
		}
		
		protected virtual void OnCountryEntryChanged (object sender, System.EventArgs e)
		{
			string country = countryEntry.Text;
			FileInfo fi = new FileInfo(appdir + "/cities/" + country + ".csv");
			if (fi.Exists)
			{
				cityEntry.Completion = new EntryCompletion();
				ListStore store = new ListStore(typeof(string), typeof(string), typeof(string));
				cityEntry.Completion.Model = store;
				cityEntry.Completion.TextColumn = 0;
				
				CellRendererText textcell1 = new CellRendererText();
				cityEntry.Completion.PackStart(textcell1, true);
				cityEntry.Completion.AddAttribute(textcell1, "text", 1);
				
				CellRendererText textcell2 = new CellRendererText();
				cityEntry.Completion.PackStart(textcell2, true);
				cityEntry.Completion.AddAttribute(textcell2, "text", 2);
				
				StreamReader sr = new StreamReader(appdir + "/cities/" + country + ".csv");
				while( ! sr.EndOfStream)
				{
					store.AppendValues(sr.ReadLine().Split(','));
				}
				
				cityEntry.Sensitive = true;
			}
			else
			{
				cityEntry.Sensitive = false;
			}
		}

		protected virtual void OnCityEntryChanged (object sender, System.EventArgs e)
		{
			string country = countryEntry.Text;
			string city = cityEntry.Text;
			string[] tab;
			
			ArrayList cities = new ArrayList();
			
			StreamReader sr = new StreamReader(appdir + "/cities/" + country + ".csv");
			while( ! sr.EndOfStream)
			{
				tab = sr.ReadLine().Split(',');
				cities.Add(tab[0]);
				if (tab[0] == city)
				{
					Chart.Location = new Location(country, tab[0], tab[1], tab[2]);
					Chart.Saved = false;
					parent.OnChartRefresh();
					break;
				}
			}
		}

		protected virtual void OnNameEntryChanged (object sender, System.EventArgs e)
		{
			if (nameEntry.Text != "")
			{
				Chart.Name = nameEntry.Text;
				Chart.Saved = false;
			}
		}

		protected virtual void OnTzComboboxChanged (object sender, System.EventArgs e)
		{
			double[] tab = new double[] {12,11,10,9.5,9,8,7,6,5,4.5,4,3.5,3,2,1,0,-1,-2,-3,-3.5,-4,-4.5,-5,-5.5,-5.75,-6,-6.5,-7,-8,-8.75,-9,-9.5,-10,-10.5,-11,-11.5,-12,-12.75,-13,-14};
			Chart.TimeZone = tab[tzCombobox.Active];
			Chart.Saved = false;
			parent.OnChartRefresh();
		}

		protected virtual void OnDstCheckbuttonClicked (object sender, System.EventArgs e)
		{
			Chart.DST = dstCheckbutton.Active;
			Chart.Saved = false;
			parent.OnChartRefresh();
		}

		protected virtual void OnDateChanged (object sender, System.EventArgs e)
		{
			Chart.DateTime = dateCalendar.Date
				.AddHours(hourSpinbutton.Value)
				.AddMinutes(minuteSpinbutton.Value)
				.AddSeconds(secondSpinbutton.Value);			
			Chart.Saved = false;
			parent.OnChartRefresh();
		}
	}
}
